<template>
  <div class="to-image">
    <el-image
      fit="cover"
      :src="props.src"
      hide-on-click-modal
      preview-teleported
      :preview-src-list="[props.src]"
      v-bind="$attrs"
      :data-id="props.src"
      v-if="props.src"
    >
      <template #placeholder>
        <slot name="placeholder">
          <div class="image-slot" style="font-size: 14px;">
            加载中<span class="dot">...</span>
          </div>
        </slot>
      </template>
      <template #error>
        <slot name="error">
          <div class="image-slot">
            <el-icon>
              <Picture />
            </el-icon>
          </div>
        </slot>
      </template>
    </el-image>
    <div class="image-slot" v-bind="$attrs" v-else>
      <el-icon>
        <Picture />
      </el-icon>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { Picture } from '@element-plus/icons-vue'

const props = defineProps({
  // 控制显示footer
  src: {
    type: String,
    default: ''
  }
})
defineExpose({
  clickHandler: () => {
    if (props.src) {
      const dom = document.querySelector(`img[data-id='${props.src}']`) as HTMLElement
      dom && dom.click()
    }
  }
})
</script>

<script lang="ts">
export default {
  inheritAttrs: false
}
</script>

<style lang="scss" scoped>
.to-image {
  .image-slot {
    display: flex;
    flex: 1;
    overflow: hidden;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background: var(--el-fill-color-light);
    color: var(--el-text-color-secondary);
    font-size: 30px;
  }
}
</style>
